<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/thor-toolkit" prefix="tt" %>
<!DOCTYPE html>
<html>
<head>
	<tt:env/>
    <title>对标管理平台</title>
    <meta charset="utf-8">
    <%@include file="../common-include.jspf"%>
    <script>
         $(function(){
           	tui.ctrl.form("getPermissionGroupForm").on("receive",function(data){
           		tui.ctrl.list("leftList").data(data);
           	});
           	
           	var reportfm=tui.ctrl.form("getReportListForm");
           	reportfm.on("receive",function(data){
           		tui.ctrl.list("rightList").data(data);
           	});
           	
           	function createReport(grid){
           		var dlg=tui.ctrl.dialog();
           		dlg.on("open",function(){
           			var fm=tui.ctrl.form("getReportSection");
           			fm.submit();
           			fm.on("receive",function(data){
           				tui.ctrl.input("reportSection").data(data);
           			});
           		});
            	dlg.showResource("newReportHtml","新建报表",
                    [{name:"保存", func: function(){
                    	var proc = function() {
                            dlg.close();
                            fm.off("success", proc);
                        };
                        var fm=tui.ctrl.form("createReportForm");  
        				fm.submitForm("getReportListForm");
        				fm.submit();
        				reportfm.once("receive",function(data){
        					grid.data(data);
                   		});
                        fm.once("success",proc);
                      }},
                    {name:"取消", func: function(){
                        dlg.close();
                    }}
               	]);
           	}
           	tui.ctrl.button("manageReportBtn").on("click",function(data){
           		var dlg=tui.ctrl.dialog();
           		var grid;
           		dlg.on("open",function(){
           			var btn=tui.ctrl.button("createReportBtn");
           			btn.on("click",function(data){
           				createReport(grid);
           			});
           			grid=tui.ctrl.grid("repostList");
           			grid.columns([{ key: 'sectionname', name:'所属板块'},
           			              { key: 'key', name:'ID',width:100,fixed: true},
                                  { name: '报表名称', key:'reportname' }
                                 ]);
           			var menu=[{key: 'delete', value: '删除'}];
           			grid.on("rowcontextmenu",function(data){
           				var id=grid.activeItem().key;
           				var m=tui.ctrl.menu(menu);
           				m.show({x: data.event.clientX, y: data.event.clientY}, "rT");
                      	m.on("select",function(data){
                      		var checked=data.item.key;
                      		if(checked==="delete"){
       							tui.askbox("确认删除此报表?","确认删除",function(result){
       								if(result){
       									var fm=tui.ctrl.form();
       									fm.id("deleteReportForm");
       									fm.action("${root}/action/sysDeleteReport");
       									fm.immediateValue({"id":id});
       									fm.submitForm("getReportListForm");
       			        				fm.submit();
       			        				reportfm.once("receive",function(data){
       			        					grid.data(data);
       			                   		});
       								}
       							});
        					}
                      	});
                      	return tui.cancelDefault(data.event);
           			});
           			reportfm.submit();
           			reportfm.once("receive",function(data){
    					grid.data(data);
               		});
           			
           		});
            	dlg.showResource("manageReportHtml","报表管理",
                    [
                    {name:"关闭", func: function(){
                        dlg.close();
                    }}
              	]);
           	});
           	var leftList=tui.ctrl.list("leftList")
           	var groupId;
          /* 	leftList.on("rowclick",function(data){
           		var item=leftList.activeItem();
           		groupId=item.key;
           		$("#reportNameSp")[0].innerHTML=item.value;
           		reportfm.method("post");
           		reportfm.action("${root}/action/sysListReport");
           		reportfm.immediateValue({"id":item.key});
           		reportfm.submit();
           	});
           	*/
           	var lastArr;
           	function listReportByGroup(id){
           		var fm=tui.ctrl.form("getGroupReportId");
           		fm.immediateValue({id:groupId});
           		fm.submit();
           		rightList.uncheckAllItems();
        		fm.on("receive",function(data){
        			var arr=[];
        			for(var i=0;i<data.length;i++){
        				arr.push(data[i].report_id);
        			}
        			lastArr=arr;
        			rightList.doCheck(arr,true);
        		});
           	}
           	var groupItem;
           	leftList.on("rowclick",function(data){
           		var item=leftList.activeItem();
           		groupItem=item;
           		groupId=item.key;
           		$("#reportNameSp")[0].innerHTML=item.value;
           		listReportByGroup(groupId);
           	});
           	
           	var rightList=tui.ctrl.list("rightList")
           	rightList.on("rowcheck",function(data){
           		var fm=tui.ctrl.form();
           		var item=rightList.activeItem();
           		if(!groupId){
           			tui.infobox("请选择组","请选择组");
           			return;
           		}
           		if(item.checked){
           			//添加
           			fm.id("addReportTogroup");
           			fm.action("${root}/action/sysAddReportToGroup");
           			tui.askbox("确定添加此报表权限?","确认权限",function(result){
           				if(result){
           					var obj="{"+groupId;
                 			if(groupItem.__parent){
             						obj=obj+","+groupItem.__parent.key +"}";
             						fm.immediateValue({"groupId":obj,"reportId":item.key});
               					
               				}else{
               					obj=obj+"}";
               					fm.immediateValue({"groupId":obj,"reportId":item.key});
               				}
           					fm.submit();
           				}
           				else{
           					item.checked=false;
           					listReportByGroup(groupId);
           				}
           			});
           		}else{
           			//删除
           			fm.id("delReportTogroup");
           			fm.action("${root}/action/sysDeleteReportFromGroup");
           			if(groupItem.children){
	           			tui.askbox("此操作将会删除此组及其子组的报表权限,<br>确认删除?","确认权限",
           				  function(result){
	           				if(result){
	           						var obj="{"+groupId+",";
	           						for(var i=0;i<groupItem.children.length;i++){
	           							if(i==groupItem.children.length-1){
	           								obj=obj+groupItem.children[i].key;
	           							}else{
	           								obj=obj+groupItem.children[i].key+",";
	           							}
	           						}
	           						obj=obj+"}";
	           						fm.immediateValue({"reportId":item.key,"groupId":obj});
	           						fm.submit();
	           					}
	           				else{
	           					item.checked=true;
	           					listReportByGroup(groupId);
	           				}
	           			});
           			}
           			else{
           				var obj="{"+groupId+"}";
   						fm.immediateValue({"reportId":item.key,"groupId":obj});
   						tui.askbox("确认删除此报表权限?","确认权限",function(result){
   							if(result){
   								fm.submit();
   							}else{
   	           					item.checked=true;
   	           					listReportByGroup(groupId);
   	           				}
   						});
   					}
   					
           		}
           	});
         });
    </script>
    <script type="text/html" id="newReportHtml">
    	<div style="padding:0 20px;width:340px">
			<span class="tui-label lable-white">板块：</span><span id="reportSection" class="tui-input"  data-type="select" data-validator= "{'*any':'请选择板块'}" data-form="addCommentsForm"></span>
			<span class="tui-label lable-white">报表ID：</span><span id="reportIdInput" class="tui-input" data-validator= "{'*any':'请填写报表ID'}" data-field="id" data-form="createReportForm"></span></br>
        	<span class="tui-label lable-white">报表名称：</span><span id="reportNameInput" class="tui-input" data-validator= "{'*any':'请填写报表名称'}" data-field="name" data-form="createReportForm"></span>
		</div>
	</script>
	<script type="text/html" id="manageReportHtml">
    	<div style="padding:0 20px;">
			<a id="createReportBtn" class="tui-button tui-primary" style="float:right">新建报表</a>
			<div style="clear:both"></div>
			<div id="repostList" class="tui-grid" style="height:400px" data-rowselectable="true"></div>
		</div>
	</script>
</head>
<body style="margin:0px;background-color:#E7E9ED;">
    <%@include file="../common-header.jspf"%>
    <div style="width:95%;margin:0 auto;min-width:1200px ;text-align:center;padding:10px 0 0 0">
	    <div style="margin:0 auto;display:inline-block;width:30%;margin-right:30px;vertical-align: middle;">
	    	<h3>用户组列表</h3>
	    	<div id="leftList" class="tui-list"  data-key-is-link="true" 
	            style="height:500px;background-color:#fafafa;
	            box-shadow: inset 1px 1px 3px rgba(100,100,100,0.1);width:100%"></div>
	    </div>
	    <div style="margin:0 auto;display:inline-block;width:30%;vertical-align: middle;">
	    	<h3 style="float:left;padding-left:60px"><span id="reportNameSp"></span>报表列表</h3>
	    	&nbsp;&nbsp;<a class="tui-button tui-primary" id="manageReportBtn" style="float:right;margin-top:15px">报表管理</a>
	    	<div id="rightList" class="tui-list"  data-key-is-link="true" 
	            style="height:500px;background-color:#fafafa;
	            box-shadow: inset 1px 1px 3px rgba(100,100,100,0.1);width:100%">
            </div>
	    </div>
	    <br/>
    </div>
</body>
	<span id="getPermissionGroupForm" class="tui-form" data-method="get" data-auto-submit="true" data-action="${root}/action/sysListPermissionGroup"></span>
	<span id="getReportListForm" class="tui-form" data-method="get"  data-auto-submit="true" data-action="${root}/action/sysListInitReport"></span>
	<span id="createReportForm" class="tui-form" data-action="${root}/action/sysCreateReport">
		<span data-field="sectionId" data-target="reportSection"></span>
		<span data-field="id" data-target="reportIdInput"></span>
		<span data-field="name" data-target="reportNameInput"></span>
	</span>
	<span id="getReportSection" class="tui-form" data-method="get" data-action="${root}/action/sysListSection"></span>
	<span id="getGroupReportId" class="tui-form" data-action="${root}/action/sysListReportId"></span>
</html>
